package com.gzw.shop.adapter;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.gzw.shop.R;
import com.gzw.shop.bean.Compaign;
import com.gzw.shop.bean.HomeCampaign;
import com.gzw.shop.fragment.CartFragment;
import com.squareup.picasso.Picasso;

import java.util.AbstractCollection;
import java.util.List;

import static android.icu.lang.UCharacter.GraphemeClusterBreak.L;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.T;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.V;

/**
 * Created by JACKYGUO on 2017-03-02.
 */

public class HomeCatgoryAdapter extends RecyclerView.Adapter<HomeCatgoryAdapter.ViewHolder> {

    private LayoutInflater mInflater;

    private static final int LEFT_TYPE = 1;
    private static final int RIGHT_TYPE = 2;

    private List<HomeCampaign> mList;
    private Context mContext;

    private OnCampaignClickListener mListener;

    public void setListener(OnCampaignClickListener listener) {
        mListener = listener;
    }

    public HomeCatgoryAdapter(List<HomeCampaign> mList, Context context) {
        this.mList = mList;
        this.mContext = context;
    }


    @Override
    public int getItemViewType(int position) {

        if (position % 2 == 0) {
            return LEFT_TYPE;
        } else {
            return RIGHT_TYPE;
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        mInflater = LayoutInflater.from(parent.getContext());
        View view;

        if (viewType == LEFT_TYPE) {
            view = mInflater.inflate(R.layout.template_home_cardview, parent, false);
        } else {
            view = mInflater.inflate(R.layout.template_home_cardview2, parent, false);
        }

        ViewHolder holder = new ViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        HomeCampaign homeCampaign = mList.get(position);
        holder.textTitle.setText(homeCampaign.getTitle());

        Picasso.with(mContext).load(homeCampaign.getCpOne().getImgUrl()).into(holder.imageViewBig);
        Picasso.with(mContext).load(homeCampaign.getCpTwo().getImgUrl()).into(holder.imageViewSmallTop);
        Picasso.with(mContext).load(homeCampaign.getCpThree().getImgUrl()).into(holder.imageViewSmallBottom);

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView textTitle;
        ImageView imageViewBig;
        ImageView imageViewSmallTop;
        ImageView imageViewSmallBottom;

        public ViewHolder(View itemView) {
            super(itemView);

            textTitle = (TextView) itemView.findViewById(R.id.text_title);
            imageViewBig = (ImageView) itemView.findViewById(R.id.imgview_big);
            imageViewSmallTop = (ImageView) itemView.findViewById(R.id.imgview_small_top);
            imageViewSmallBottom = (ImageView) itemView.findViewById(R.id.imgview_small_bottom);

            imageViewBig.setOnClickListener(this);
            imageViewSmallTop.setOnClickListener(this);
            imageViewSmallBottom.setOnClickListener(this);

        }

        @Override
        public void onClick(View view) {

            HomeCampaign homeCampaign = mList.get(getLayoutPosition());
            if (mListener != null) {
                anim(view);
            }

        }

        //给点击事件增加一个动画效果
        private void anim(final View view) {
            ObjectAnimator animator = ObjectAnimator.ofFloat(view, "rotationX", 0.0F, 360.0F)
                    .setDuration(200);
            animator.addListener(new AnimatorListenerAdapter() {

                @Override
                public void onAnimationEnd(Animator animator) {

                    HomeCampaign campaign = mList.get(getLayoutPosition());

                    switch (view.getId()) {
                        case R.id.imgview_big:
                            mListener.onClick(view, campaign.getCpOne());
                            break;
                        case R.id.imgview_small_top:
                            mListener.onClick(view, campaign.getCpTwo());
                            break;
                        case R.id.imgview_small_bottom:
                            mListener.onClick(view, campaign.getCpThree());
                            break;
                    }

                }
            });

            animator.start();
        }
    }

    public interface OnCampaignClickListener {
        void onClick(View view, Compaign compaign);
    }


}
